package com.xavier.book.spring_book_demo.controller;

import com.xavier.book.spring_book_demo.constant.Constants;
import com.xavier.book.spring_book_demo.model.UserInfo;
import com.xavier.book.spring_book_demo.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public Boolean login(String username, String password, HttpSession session) {

        //1.参数校验
        //2.校验账号密码是否真确
        //3.设置session
        //4.返回结果
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        //根据用户名，获取用户信息
        UserInfo userInfo = userInfoService.queryUserInfoByName(username);
        if (userInfo == null) {
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            session.setAttribute(Constants.SESSION_USER_KEY,userInfo);
            return true;
        }
        return false;
    }
}
